<?php

/**
 * This is the model class for table "tests".
 *
 * The followings are the available columns in table 'tests':
 * @property integer $tests_id
 * @property string $tests_name
 * @property integer $tests_score
 * @property string $tests_memo
 * @property integer $tests_creater_id
 * @property string $tests_create_time
 * @property string $tests_edit_time
 */
class Tests extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Tests the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tests';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tests_name', 'length', 'max'=>50),
			//不允许为空
			array('tests_name,tests_score,tests_memo','required'),
			//总分只能是0-100
			array('tests_score','numerical'),
			//array('tests_score','numerical','min'=>'0','max'=>'100'),
			array('tests_name, tests_score,tests_memo', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('tests_id, tests_name, tests_score, tests_memo, tests_creater_id, tests_create_time, tests_edit_time', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'author'=>array(self::BELONGS_TO,'User','tests_creater_id'),
			'subjects'=>array(self::HAS_MANY,'Subjects','subjects_tests_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'tests_id' => '试卷管理',
			'tests_name' => '试卷名称',
			'tests_score' => '试卷总分',
			'tests_memo' => '试卷说明',
			'tests_creater_id' => '创建人',
			'tests_create_time' => '创建时间',
			'tests_edit_time' => '修改时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('tests_id',$this->tests_id);

		$criteria->compare('tests_name',$this->tests_name,true);

		$criteria->compare('tests_score',$this->tests_score);

		$criteria->compare('tests_memo',$this->tests_memo,true);

		$criteria->compare('tests_creater_id',$this->tests_creater_id);

		$criteria->compare('tests_create_time',$this->tests_create_time,true);

		$criteria->compare('tests_edit_time',$this->tests_edit_time,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
	public function beforeSave() {
		if(parent::beforeSave()){
			if($this->isNewRecord){
				$this->tests_creater_id = Yii::app()->user->id;
				$this->tests_create_time = $this->tests_edit_time = date("Y-m-d H:i:s");
			}else{
				$this->tests_edit_time = date("Y-m-d H:i:s");
			}
			return true;
		}else{
			return false;
		};
	}
	public function filterData()
	{
		if(Yii::app()->user->getState('roles') != 0)
		{
			$this->tests_creater_id = Yii::app()->user->id;
		}
	}
	
}